﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Restbucks.Shared.Representations;
using Restbucks.Shared.Domain;
using Restbucks.Shared.Mappers;
using Restbucks.Server.Resources;

namespace Restbucks.Server.Activities
{
    public interface ICreateOrderActivity { };

    class CreateOrderActivity : ICreateOrderActivity
    {
        private readonly OrderRepository _repository;

        public CreateOrderActivity(OrderRepository repository)
        {
            _repository = repository;
        }

        //public OrderRepresentation create(Order order, RestbucksUri requestUri)
        //{
        //    order.setStatus(OrderStatus.UNPAID);

        //    Identifier identifier = OrderRepository.current().store(order);

        //    RestbucksUri orderUri = new RestbucksUri(requestUri.getBaseUri() + "/order/" + identifier.toString());
        //    RestbucksUri paymentUri = new RestbucksUri(requestUri.getBaseUri() + "/payment/" + identifier.toString());

        //    return new OrderRepresentation(order,
        //        new Link(Representation.RELATIONS_URI + "cancel", orderUri),
        //        new Link(Representation.RELATIONS_URI + "payment", paymentUri),
        //        new Link(Representation.RELATIONS_URI + "update", orderUri),
        //        new Link(Representation.SELF_REL_VALUE, orderUri));
        //}

        private readonly OrderRepresentationMapper _orderRepresentationMapper = new OrderRepresentationMapper();

        public OrderRepresentation Create(OrderRepresentation orderRepresentation, Uri requestUri)
        {
            var orderReq = OrderRepresentationMapper.GetDomainObject(orderRepresentation);
//            orderReq.Status = OrderStatus.Unpaid;
            var order = _repository.Store(orderReq);

            var orderUri = RestbucksResources.GetResourceUri<OrderResource>(requestUri, order.ToString());
//            var paymentUri = RestbucksResources.GetResourceUri<PaymentResource>(requestUri, orderId.ToString());

            var result = OrderRepresentationMapper.GetRepresentation(orderReq);
            result.UpdateLink = orderUri;
            result.SelfLink = orderUri;
            result.CancelLink = orderUri;
//            result.PaymentLink = paymentUri;
            return result;
        }

    }
}
